﻿using Colorful;
using Colorful.Database;
using Colorful.MemberSystem;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MemberSystemDb
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateDatabase();
        }


        static void CreateDatabase()
        {
            using (var db = DbHelper.GetDb())
            {
                var mdbHelper = new ConsoleApp2.MDBHelp(@"E:\MemberDB.mdb");
                mdbHelper.Open();
                var data = mdbHelper.GetDataSet(@"select * from members");
                if (data.Tables.Count > 0 && data.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in data.Tables[0].Rows)
                    {
                        var cardId = CommonHelper.ToBanJiao($"{row["CardID"]}");
                        if (db.Members.Any(a => a.CardId == cardId))
                            continue;
                        var member = new Colorful.MemberSystem.Models.Member();
                        member.CardId = $"{row["CardID"]}";
                        member.CardType = Convert.ToInt32(row["CardType"]);
                        member.Name = $"{row["UserName"]}";
                        member.Sex = $"{row["Sex"]}";
                        member.Birthday = (DateTime)row["Birthday"];
                        member.Tel = $"{row["Tel"]}";
                        member.Mobile = $"{row["Mobile"]}";
                        member.QQ = $"{row["QQ"]}";
                        member.Address = $"{row["Address"]}";
                        member.Money = (decimal)row["Money"];
                        member.Point = Convert.ToInt32(row["Point"]);
                        member.Remark = $"{row["Remark"]}";
                        member.AddDate = (DateTime)row["AddDate"];
                        member.LastModify = (DateTime)row["ModifyDate"];
                        member.ModifyUser = $"{row["OpUser"]}";
                        member.LastRecharge = (DateTime)row["RechargeDate"];
                        if (row["RechargeYear"] == DBNull.Value)
                            member.ExpirationDate = member.LastRecharge;
                        else
                            member.ExpirationDate = member.LastRecharge.AddYears(Convert.ToInt32(row["RechargeYear"]));
                        db.Members.Add(member);
                    }
                    mdbHelper.Close();
                }
            }
        }

    }
}
